// pages/tweet/publish/publish.js
import { 
  TWEET_PUB
 } from "../../../request/api";

 import { 
  _qqEmojiMap
  } from "../../../component/special_text/emoji";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content: '',
    aboutFromTweetId: 0,
    aboutId: 0,
    aboutType: 0,
    imagesList: '',
    showEmoji: false,
    emojiList: [],
    selectedUsers: [],
    reloadByAt: false,
    reloadByTopic: false,
    topicTitle: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var list = [];
    for (var key of Object.keys(_qqEmojiMap)) {
      list = list.concat({'name': key, 'imagePath': '/emoji/'+_qqEmojiMap[key]+'.png'})
    }
    var that = this;
    that.setData({
      emojiList: list,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var hasAt = this.data.reloadByAt && this.data.selectedUsers.length > 0;
    var hasTopic = this.data.reloadByTopic && this.data.topicTitle.length > 0;
    if(hasAt || hasTopic){
      var tempString = '';
      if(hasAt){
        this.data.selectedUsers.forEach(element => {
          tempString += '@' + element.name + ' ';
        });
      } else {
        tempString = '#' + this.data.topicTitle + '# ';
      }

      var tempContent = this.data.content.length == 0? tempString : this.data.content + ' ' + tempString;
      this.setData({
        content: tempContent,
        reloadByAt: false,
        selectedUsers: [],
        reloadByTopic: false,
        topicTitle: '',
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  inputFocus: function(){
    this.setData({
      showEmoji: false,
    });
  },

  inputChange: function(e){
    var tempContent = e.detail.value;
    this.setData({
      content: tempContent,
    });
  },

  viewClick: function(e){
    var id = e.currentTarget.id;
    switch(id){
      case '0':
        this.setData({
          showEmoji: false,
        });
        break;
      case '1':
        this.navigateTo('./at/at');
        this.setData({
          showEmoji: false,
        });
        break;

      case '2':
        this.setData({
          showEmoji: false,
        });
        this.navigateTo('./topic/topic');
        break;

      case '3':
        this.setData({
          showEmoji: !this.data.showEmoji,
        });
        break;
    }
  },

  tapEmoji: function(e){
    var name = e.currentTarget.dataset.name;
    var tempContent = this.data.content.length == 0? name : this.data.content + ' ' + name;
    this.setData({
      content: tempContent,
    });
  },

  navigateTo: function(url){
    wx.navigateTo({
      url: url,
    });
  },


  showTips: function (msg) {
    wx.showToast({
      title: msg,
      icon: 'none'
    })
  },

  send: function(){
    if(!this.data.content){
      this.showTips('请输入内容');
      return;
    } else if(this.data.content.length > 160){
      this.showTips('输入内容过长');
      return;
    }
    this.showLoading('正在提交...');
    TWEET_PUB(
      {
        'aboutFromTweetId': this.data.aboutFromTweetId,
        'aboutId': this.data.aboutId,
        'aboutType': this.data.aboutType,
        'content': this.data.content,
        'imagesList': this.data.imagesList,
      }
    ).then(res => {
      this.hideLoading();
      this.showTips(res.data.message);
      if(res.data.code == 1){
        wx.navigateBack();
      }
    }).catch(err => {
      this.hideLoading();
    });
  },

  showLoading: function(msg){
    wx.showLoading({
      title: msg,
    });
  },
  hideLoading: function(){
    wx.hideLoading({
      success: (res) => {},
    });
  },


})